#!/bin/bash
# 统计pid行数
pidCount=`ps -ef|grep xyz-framework-api|grep -v "grep"|wc -l`
#启动的时候是3 没有启动是 0 应该和脚本命名有关
echo $pidCount
export CLASSPATH=$CLASSPATH:/shzhyun/frameworkapi/extLibs
if [ "$pidCount" = "0" ]; then
    echo "进程未运行，启动..."
    current=`date "+%Y%m%d%H%M%S"`
    mv /shzhyun/frameworkapi/logs/info.log /shzhyun/frameworkapi/logs/info.log.$current
    echo "日志备份完成 $current"
    LOG_HOME=/shzhyun/frameworkapi/logs
    JAR_HOME=/shzhyun/frameworkapi/xyz-framework-api-1.0.1.jar
    nohup java -Dspring.profiles.active=prod -jar $JAR_HOME >$LOG_HOME/info.log 2>&1 &
    tail -200f $LOG_HOME/info.log
    echo '启动完成...!'
elif [ "$pidCount" = "1" ]; then
  echo " 进程运行中....."
	true_pid=`ps -ef|grep xyz-framework-api |grep -v "grep"|awk '{print $2}'`
	echo "pid = $true_pid"
    kill -9 $true_pid
    echo "进程重新运行，启动..."
    current=`date "+%Y%m%d%H%M%S"`
    mv /shzhyun/frameworkapi/logs/info.log /shzhyun/frameworkapi/logs/info.log.$current
    echo "日志备份完成 $current"
    LOG_HOME=/shzhyun/frameworkapi/logs
    JAR_HOME=/shzhyun/frameworkapi/xyz-framework-api-1.0.1.jar
    nohup java -Dspring.profiles.active=prod -jar $JAR_HOME >$LOG_HOME/info.log 2>&1 &
    tail -200f $LOG_HOME/info.log
    echo '启动完成...!'
fi


